﻿//------------------------------------------------------------------------------------------------------
// The MIT License (MIT) Copyright (c) 2014 Marton Rusko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
// associated documentation files (the "Software"), to deal in the Software without restriction, 
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial 
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//------------------------------------------------------------------------------------------------------
using System;
using RM.Banking.Unicredit.Statements.Interfaces;

namespace RM.Banking.Unicredit.Statements
{
    /// <summary>
    /// Unicredit statmenet record for data records
    /// </summary>
    public class UnicreditStatementDataRecord : UnicreditStatementRecord, 
                                                IHasExtenderRow, ISetExtenderRow
    {
        #region Private fields

        //HACK: Fields initialized with reflection
#pragma warning disable 649

        /// <summary>
        /// A fiók kódja
        /// </summary>
        [UnicreditRowMapping(2, 4)]
        private string fiokKodja;

        /// <summary>
        /// Számlaszám
        /// </summary>
        [UnicreditRowMapping(6, 24)]
        private string szamlaszam;

        /// <summary>
        /// A kivonat dátuma(YYYYMMDD alakban)
        /// </summary>
        [UnicreditRowMapping(30, 8)]
        private string kivonatDatuma;

        /// <summary>
        /// A tétel sorszáma
        /// </summary>
        [UnicreditRowMapping(38, 7)]
        private string tetelSorszama;

        /// <summary>
        /// Könyvelési referencia
        /// </summary>
        [UnicreditRowMapping(45, 14)]
        private string konyvelesiReferencia;

        /// <summary>
        /// Data rekord típusjelző
        /// </summary>
        [UnicreditRowMapping(59, 2)]
        private string dataRekordTipusjelzo;

        /// <summary>
        /// Narratív vagy kiegészítő rekord referencia
        /// </summary>
        [UnicreditRowMapping(61, 35)]
        private string kiegeszitoRekordRefrencia;

        /// <summary>
        /// Könyvelési nap(YYYYMMDD alakban)
        /// </summary>
        [UnicreditRowMapping(96, 8)]
        private string konyvelesiNap;

        /// <summary>
        /// Értéknap(YYYYMMDD alakban)
        /// </summary>
        [UnicreditRowMapping(104, 8)]
        private string erteknap;

        /// <summary>
        /// Összeg előjele
        /// </summary>
        [UnicreditRowMapping(112, 1)]
        private string osszegElojele;

        /// <summary>
        /// Összeg
        /// </summary>
        [UnicreditRowMapping(113, 15)]
        private string osszeg;

        /// <summary>
        /// Jogcímkód
        /// </summary>
        [UnicreditRowMapping(126, 3, Optional = true)]
        private string jogcimkod;

        /// <summary>
        /// Országkód
        /// </summary>
        [UnicreditRowMapping(129, 2, Optional = true)]
        private string orszagkod;

#pragma warning restore 649

        #endregion

        #region Public properties

        #region Implementation of IHasExtenderRow

        public UnicreditStatementRecord ExtenderRecord { get; private set; }

        #endregion

        #region Implementation of ISetExtenderRow

        UnicreditStatementRecord ISetExtenderRow.ExtenderRecord { set { ExtenderRecord = value; } }

        #endregion

        #region Data properties

        public string FiokKodja { get; private set; }
        public string Szamlaszam { get; private set; }
        public DateTime KivonatDatuma { get; private set; }
        public string TetelSorszama { get; private set; }
        public string KonyvelesiReferencia { get; private set; }
        public UnicreditStatementDataRecordType DataRekordTipusjelzo { get; private set; }
        public string KiegeszitoRekordRefrencia { get; private set; }
        public DateTime KonyvelesiNap { get; private set; }
        public DateTime Erteknap { get; private set; }
        public string OsszegElojele { get; private set; }
        public double Osszeg { get; private set; }
        public string Jogcimkod { get; private set; }
        public string Orszagkod { get; private set; }

        #endregion

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a UnicreditStatementDataRecord
        /// </summary>
        /// <param name="line">Statement line</param>
        public UnicreditStatementDataRecord(string line)
            : base(line)
        {
            DataRekordTipusjelzo = (UnicreditStatementDataRecordType)int.Parse(dataRekordTipusjelzo);

            if (DataRekordTipusjelzo != UnicreditStatementDataRecordType.ExtendedTextItem)
            {
                FiokKodja = fiokKodja;
                Szamlaszam = szamlaszam;
                KivonatDatuma = ParseDate(kivonatDatuma);
                TetelSorszama = tetelSorszama;
                KonyvelesiReferencia = konyvelesiReferencia;
                KiegeszitoRekordRefrencia = kiegeszitoRekordRefrencia;
                KonyvelesiNap = ParseDate(konyvelesiNap);
                Erteknap = ParseDate(erteknap);
                OsszegElojele = osszegElojele;
                Osszeg = ParseAmount(osszeg, osszegElojele);
                Orszagkod = orszagkod;
                Jogcimkod = jogcimkod;
            }
            else
            {
                KiegeszitoRekordRefrencia = kiegeszitoRekordRefrencia;
            }
        }

        #endregion

        #region Implementation of IHasExtenderRow

        /// <summary>
        /// This record contains any extensions?
        /// </summary>
        /// <returns></returns>
        public bool HasExtension()
        {
            return DataRekordTipusjelzo == UnicreditStatementDataRecordType.HasExtensionItem;
        }

        #endregion
    }
}